package com.wxg.mq.ch02;

import java.io.UnsupportedEncodingException;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;

public class SyncProducer {
	public static final String GROUP_NAME = "g_wxg";
	public static final String NAMESRV_ADDR = "127.0.0.1:9876";
	
	public static final String TOPIC = "TopicTest";
	
	public static void main(String[] args) throws MQClientException, UnsupportedEncodingException, RemotingException, MQBrokerException, InterruptedException {
		// Instantiate with a Producer group name.
		DefaultMQProducer producer = 
				new DefaultMQProducer(GROUP_NAME);
		producer.setNamesrvAddr(NAMESRV_ADDR);
		
		// Launch the instance.
		producer.start();
		
		for (int i = 0; i < 100; i++) {
			// Create a Message instance, specifying Topic, tag and Message body.
			Message msg = new Message(TOPIC, 
					"TagA",
					("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
			// Call send Message to deliver Message to one of brokers.
			SendResult sendResult = producer.send(msg);
			System.out.printf("%s%n", sendResult);
		}
		
		producer.shutdown();
	}
}
